var json = {
    div_container:{
        type: 'div',
        width: '183px',
        height: '133px',
        backgroundColor: '',
        border: '1px solid #000',
        cssFloat: 'left'
    },

    img_folder:{
        type: 'img',
        width: '85%',
        height: '85%'
    },

    par_name:{
        type: 'par'
    }
}

const fs = require('fs');
const path = require('path');
var div_main = CreateMainDivElement();    //加载Maindiv
document.body.appendChild(div_main);

// var currentFolderPath = 'G:/Painting';
var currentFolderPath = fs.readFileSync('./Extensions/currentPath.txt', 'utf8');


var showProjectPath =  document.getElementById('projectPath')

var gotoButton = document.getElementById('gotoButton');
gotoButton.onclick = function(){
    var orign = currentFolderPath;
    currentFolderPath = showProjectPath.value;
    ReadFolder('');
}

var returnButton = document.getElementById('returnButton');
returnButton.addEventListener('click', function(){
    currentFolderPath = path.resolve(currentFolderPath, '../');
    ReadFolder('');
});


function ReadFolder(folderName){
    currentFolderPath = path.join(currentFolderPath, folderName);
    showProjectPath.value = currentFolderPath;
    fs.writeFileSync('./Extensions/currentPath.txt', currentFolderPath.toString(), 'utf8');

    fs.readdir(currentFolderPath, function(err, files){
        if(err){
            alert(err);
            return false;
        }else{
            div_main.innerHTML = '';
            var obj = JugdeDirectory(files);

            //加载文件夹：
            for(var i=0; i<obj.folderArray.length; i++){
                var div_container = CreateDivElement(json.div_container);       //加载文件夹container
                var img_folder = CreateImgElement(json.img_folder, './lib/folder-min.png');
                var par_name = CreateParElement(json.par_name, obj.folderArray[i]);
                div_container.appendChild(img_folder);
                div_container.appendChild(par_name, obj.folderArray.length);
                div_main.appendChild(div_container);
                div_container.addEventListener('dblclick', function(){
                    ReadFolder(this.children[1].innerHTML)
                });
            }

            //加载文件
            for(var i=0; i<obj.fileArray.length; i++){
                var str = obj.fileArray[i].split('.');
                if(str[1]=='jpg'||str[1]=='png'){
                    var div_container = CreateDivElement(json.div_container);
                    div_container.str = str;
                    var img_folder = CreateImgElement(json.img_folder, path.join(currentFolderPath, obj.fileArray[i]));
                    var par_name = CreateParElement(json.par_name, str[0].substring(0,22));
                    div_container.appendChild(img_folder);
                    div_container.appendChild(par_name, obj.folderArray.length);
                    div_main.appendChild(div_container);
                    div_container.addEventListener('dblclick', function(){
                        var cs = new CSInterface();
                        var a = currentFolderPath.replace(/\\/g, '\\\\')+'\\\\'+this.str[0];   //psd文件路径
                        cs.evalScript('ImportPSD("'+a+'")')
                    });
                }
            }
            return true;
        }
    });
}



function JugdeDirectory(files){     //判断数组内是否为文件夹，返回文件夹和文件数组
    if(files == null){
        console.log('Error, Import is not an Array!');
        return -1;
    }else{
        var fileAndFolderArray = new Object();
        fileAndFolderArray.folderArray = new Array();
        fileAndFolderArray.fileArray = new Array();
        for(var i=0; i<files.length; i++){
            var stat = fs.lstatSync(path.join(currentFolderPath, files[i]));
            if(stat.isDirectory()== true){
                fileAndFolderArray.folderArray.push(files[i]);
            }else{
                fileAndFolderArray.fileArray.push(files[i]);
            }
        }

        // console.log('All:', files);
        // console.log('Folder:', fileAndFolderArray.folderArray);
        // console.log('Files:', fileAndFolderArray.fileArray);
        return fileAndFolderArray;
    }
}

function CreateMainDivElement(){
    var div = document.createElement('div');
    div.style.margin = '0px';
    div.style.padding = '0px';
    div.style.height = '100%';
    div.style.width = '100%';
    return div;
}

function CreateDivElement(json){    //创建DIV标签
    if(json.type == 'div'){
        var div = document.createElement('div');
        div.className = 'div_container';
        div.style.margin = '0px';
        div.style.padding = '0px';
        div.style.width = json.width;
        div.style.height = json.height;
        div.style.backgroundColor = json.backgroundColor;
        div.style.border = json.border;
        div.style.cssFloat = json.cssFloat;
        div.style.textAlign = 'center';

        div.addEventListener('mouseover', function(){
             if(this.style.borderColor!='green'){
                this.style.borderColor = 'white';
             }
        });

       div.addEventListener('mouseout', function(){
             if(this.style.borderColor!='green'){
                this.style.borderColor = 'black';
               }
        });

        div.addEventListener('click', function(){
             var div = document.getElementsByClassName('div_container');
             for(var i=0; i<div.length; i++){
                div[i].style.borderColor = 'black';
             }
           this.style.borderColor = 'green';
        });
        return div;
    }else{
        return -1;
    }
    
}

function CreateImgElement(json, imgsrc){
    if(json.type == 'img'){
        var img = document.createElement('img');
        img.style.margin = '0px';
        img.style.padding = '0px';
        img.style.position = 'relative';
        img.src = imgsrc;
        img.style.width = json.width;
        img.style.height = json.height;
        return img;
    }else{
        return -1;
    }
}

function CreateParElement(json, text){
    if(json.type == 'par'){
        var par = document.createElement('p');
        par.innerHTML = text;
        par.style.color = 'white';
        return par;
    }else{
        alert('Error');
        return -1;
    }
}

ReadFolder('');

